import {defineStore} from "pinia";
import axios from "@/utils/NetWork.js";

const initData = ()=>[
    {
        data:"data1",
        type:"video",
        url:"https://www.youtube.com/watch?v=4",
        author:"YJH"
    },
    {
        data:"data1",
        type:"video",
        url:"https://www.youtube.com/watch?v=4",
        author:"YJH"
    },
    {
        data:"data1",
        type:"video",
        url:"https://www.youtube.com/watch?v=4",
        author:"YJH"
    },
    {
        data:"data1",
        type:"video",
        url:"https://www.youtube.com/watch?v=4",
        author:"YJH"
    },
    {
        data:"data001",
        type:"movie",
        url:"https://www.youtube.com/watch?v=4",
    },
    {
        data:"data2",
        type:"image",
        url:"http://localhost:8080"
    },
    {
        data:"data2",
        type:"image",
        url:"http://localhost:8080"
    },
    {
        data:"data2",
        type:"image",
        url:"http://localhost:8080"
    },
    {
        data:"data2",
        type:"image",
        url:"http://localhost:8080"
    },
    {
        data:"data2",
        type:"audio",
        url:"http://localhost:8080"
    },
    {
        data:"data2",
        type:"audio",
        url:null
    },

]

const mediaStore = defineStore("mediaStore", {
    state:()=>{
        return {
            entry:{ // 文件缩略图条目
                image:[
                    {
                        page:0,
                        filterItem:[],
                        content:[]
                    }
                ]
            },
            data:initData(),
            entryIndex:0,
            activeName:"image",
            time:null,
            date:null

        }
    },
    getters:{
        filterData:state=> {
            let data = state.data.filter((item) => item?.type === state.activeName);

            // 依次过滤
            if(state.time!=='all'){

            }
            if(state.data!=='all'){

            }
            return data
        },
        currentEntry:state=>state.entry[state.activeName][state.entryIndex].content,
    },
    actions:{
        async getEntry(options){
            const {isFlash} = options;
            // 如果待更新，有新的上传文件，则直接重置当前状态
            if(isFlash){
                console.log("有新数据")
                this.date = null;
                this.time = null;
                const current = this.entry[this.activeName];
                current.splice(0,current.length,{page:0,filterItem:[],content:[]});
                this.entryIndex = 0;
                return this.updateEntry();
            }
            // 先找是否有符合要求的
            const result = this.entry[this.activeName].filter((item,index) => {
                if (item.filterItem.includes(this.time)&&item.filterItem.includes(this.date)) {
                    this.entryIndex = index;
                    console.log("已经有类似数据,无需请求")
                    return true;
                }
            });
            // 如果没有符合要求的
            if(result.length===0){
                console.log("没有类似数据，需要请求")
                // 先推一个空数组待命
                this.entry[this.activeName].push({
                    page:0,
                    filterItem:[this.time,this.date],
                    content:[]
                })
                this.entryIndex= this.entry[this.activeName].length-1;
                // 请求数据放入空数组|更新数组
                return this.updateEntry();
            }
        },
        async updateEntry(){
            const url = `/preview/image/entry?page=${this.entry[this.activeName][this.entryIndex].page+1}&date=${this.date}&time=${this.time}`;
            return await axios.get(url)
                .then(res => {
                    // 如果请求成功则更新状态
                    this.entry[this.activeName].page +=1;
                    return res.data
                })
                .then(res=>{
                    this.entry[this.activeName][this.entryIndex].content = [...this.entry[this.activeName][this.entryIndex].content,...res];
                    console.log("entry:",this.entry[this.activeName][this.entryIndex].content)
                })
        },
        async fetchFile(){
            axios.post("http://localhost:3000/media/",{
                type:this.activeName,
            }).then(res=>console.log(res))
        }
    }
})

export default mediaStore;